import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gameplay_flutter/lib_user/user_manager.dart';
import 'package:gameplay_flutter/m_login/os_login_page.dart';
import 'package:gameplay_flutter/m_me/constants/me_assets_constants.dart';
import 'package:get/get.dart';

class MeSettingPage extends StatelessWidget {
  const MeSettingPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 3.0,
        shadowColor: Colors.grey.withOpacity(0.05),
        surfaceTintColor: Colors.transparent,
        centerTitle: true,
        title: Text(
          'Settings',
          style: TextStyle(
            fontSize: 22.sp,
            color: Color(0xFF1B1B1B),
          ),
        ),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, color: Colors.black, size: 20),
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
      body: Container(
        padding: EdgeInsets.only(top: 13.h, left: 15.w, right: 15.w),
        color: const Color(0xFAFAFAFA),
        child: Column(
          children: [
            SizedBox(
              height: 13.h,
            ),
            Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(8.r),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.03), // 阴影颜色
                      spreadRadius: 2, // 阴影扩散半径
                      blurRadius: 2, // 阴影模糊半径
                      offset: Offset(0, 2), // 阴影偏移量
                    ),
                  ],
                ),
                padding: EdgeInsets.only(left: 15.w, right: 15.w),
                child: Column(children: [
                  _buildSettingItem2(
                    icon: MeAssetsConstants.iconNotifications,
                    title: 'Notifications',
                  ),
                  Divider(
                    height: 0.5.h,
                    thickness: 0.5,
                    color: const Color(0xFFE4E4E4),
                  ),
                  _buildSettingItem2(
                    icon: MeAssetsConstants.iconPrivacy,
                    title: 'Privacy',
                  )
                ])),
            SizedBox(
              height: 16.h,
            ),
            Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(8.r),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.03), // 阴影颜色
                      spreadRadius: 2, // 阴影扩散半径
                      blurRadius: 2, // 阴影模糊半径
                      offset: Offset(0, 2), // 阴影偏移量
                    ),
                  ],
                ),
                padding: EdgeInsets.only(left: 15.w, right: 15.w),
                child: Column(children: [
                  _buildSettingItem3(
                    icon: MeAssetsConstants.iconAccount,
                    title: 'Account',
                  ),
                  Divider(
                    height: 0.5.h,
                    thickness: 0.5,
                    color: const Color(0xFFD8D8D8),
                  ),
                  _buildSettingItem2(
                    icon: MeAssetsConstants.iconCancelAccount,
                    title: 'Cancel Account',
                  )
                ])),
            SizedBox(
              height: 16.h,
            ),
            Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(8.r),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.03), // 阴影颜色
                      spreadRadius: 2, // 阴影扩散半径
                      blurRadius: 2, // 阴影模糊半径
                      offset: Offset(0, 2), // 阴影偏移量
                    ),
                  ],
                ),
                padding: EdgeInsets.only(left: 15.w, right: 15.w),
                child: Column(children: [
                  _buildSettingItem2(
                    icon: MeAssetsConstants.iconBlockedList,
                    title: 'Blocked List',
                  ),
                  Divider(
                    height: 0.5.h,
                    thickness: 0.5,
                    color: const Color(0xFFD8D8D8),
                  ),
                  _buildSettingItem2(
                    icon: MeAssetsConstants.iconClearChattingRecords,
                    title: 'Clear Chatting Records',
                  ),
                  Divider(
                    height: 0.5.h,
                    thickness: 0.5,
                    color: const Color(0xFFD8D8D8),
                  ),
                  _buildSettingItem2(
                    icon: MeAssetsConstants.iconAbout,
                    title: 'About Yahabbi',
                  )
                ])),
            _buildLogoutButton(context),
          ],
        ),
      ),
    );
  }

  Widget _buildSettingItem2({
    required String icon,
    required String title,
  }) {
    return Container(
      height: 56.h,
      child: Row(
        children: [
          Image.asset(
            icon,
            width: 22.w,
            height: 22.h,
          ),
          SizedBox(
            width: 7.w,
          ),
          Text(
            title,
            style: TextStyle(fontSize: 16.sp),
          ),
          Spacer(),
          Icon(Icons.chevron_right, color: Color(0xFFA8A8A8)),
        ],
      ),
    );
  }

  Widget _buildSettingItem3({
    required String icon,
    required String title,
  }) {
    return Container(
      height: 56.h,
      child: Row(
        children: [
          Image.asset(
            icon,
            width: 22.w,
            height: 22.h,
          ),
          SizedBox(
            width: 7.w,
          ),
          Text(
            title,
            style: TextStyle(fontSize: 16.sp),
          ),
          Spacer(),
          Image.asset(
            MeAssetsConstants.iconUnprotected,
            width: 16.w,
            height: 16.h,
          ),
          Text(
            "unprotected",
            style: TextStyle(fontSize: 14.sp, color: Color(0xFFFFA05C)),
          ),
          Icon(Icons.chevron_right, color: Color(0xFFA8A8A8)),
        ],
      ),
    );
  }

  Widget _buildLogoutButton(BuildContext context) {
    return InkWell(
      onTap: () {
        _showLogoutDialog1(context);
      },
      child: Container(
        margin: EdgeInsets.only(top: 30.h),
        width: double.infinity,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8.r),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.03), // 阴影颜色
              spreadRadius: 2, // 阴影扩散半径
              blurRadius: 2, // 阴影模糊半径
              offset: Offset(0, 2), // 阴影偏移量
            ),
          ],
        ),
        padding: const EdgeInsets.symmetric(vertical: 16),
        child: Center(
          child: TextButton.icon(
            onPressed: () {
              _showLogoutDialog1(context);
            },
            icon: const Icon(Icons.logout, color: Color(0xFF8A72FE)),
            label: Text(
              'Log Out',
              style: TextStyle(color: Color(0xFF8A72FE), fontSize: 18.sp),
            ),
            style: TextButton.styleFrom(
              minimumSize: Size.zero,
              padding: EdgeInsets.zero,
              tapTargetSize: MaterialTapTargetSize.shrinkWrap,
            ),
          ),
        ),
      ),
    );
  }

  void _showLogoutDialog1(BuildContext context) {
    showDialog(
        context: context,
        builder: (context) => Center(
            child: Container(
                width: 288.w,
                height: 130.h,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(8.r),
                ),
                child: Column(children: [
                  SizedBox(height: 17.h),
                  Text(
                    'Log Out',
                    style: TextStyle(fontSize: 18.sp, color: Colors.black),
                  ),
                  SizedBox(height: 12.h),
                  Text(
                    'Are you sure you want to log out?',
                    style: TextStyle(fontSize: 12.sp, color: Color(0xFFA8A8A8)),
                  ),
                  Spacer(),
                  Row(
                    children: [
                      Expanded(
                        flex: 1,
                        child: TextButton(
                          onPressed: () {
                            Navigator.of(context).pop();
                          },
                          child: Text(
                            'Cancel',
                            style: TextStyle(
                                fontSize: 16.sp, color: Color(0xFFC1C1C1)),
                          ),
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: TextButton(
                          onPressed: () {
                            Navigator.of(context).pop(); // 关闭对话框
                            // 执行退出操作
                            Get.offAll(() => OSLoginPage());
                            UserManager.instance.logout();
                          },
                          child: Text('Log Out',
                              style: TextStyle(
                                  fontSize: 16.sp, color: Color(0xFF1B1B1B))),
                        ),
                      )
                    ],
                  )
                ]))));
  }
}
